<template>
  <Spin :spinning="loading" tip="Loading...">
    <div>
      <!-- 直营订单开票申请财务审核-->
      <TopFilter
        :list="topFilterList"
        :cols="3"
        @filterChange="changeHandle"
        @onCollapse="collapseHandle"
        :labelWidth="110"
        @resetChange="resetChange"
      ></TopFilter>

      <Spin :spinning="exportLoading" tip="Loading...">
        <FilterTable
          ref="table"
          :columns="columns"
          :fetchapi="fetchApi"
          :params="fetchParams"
          :datakey="'records'"
          columnsRef="zjDirectSaleAudit"
          :is-select-column="true"
          :onRowSelectChange="selectChangeHandle"
          :onColumnsChange="columns => (this.columns = columns)"
        >
          <template slot="controls" slot-scope="props">
            <multiuse-button size="small" type="primary" @click="adoptHandle(60631004)">批量通过</multiuse-button>
            <multiuse-button size="small" type="primary" @click="adoptHandle(60631003)">批量驳回</multiuse-button>
            <multiuse-button size="small" type="primary" @click="addHandle()">导 出</multiuse-button>
          </template>
        </FilterTable>
      </Spin>

      <BaseDialog
        :visible.sync="issearch"
        :title="'经销商查询'"
        destroyOnClose
        :containerStyle="{ height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px' }"
      >
        <ownerSeacher :rowData="formData" @close="dealercloseHandler" />
      </BaseDialog>
      <BaseDialog
        :visible.sync="isreject"
        :title="'驳回'"
        destroyOnClose
        :containerStyle="{ height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px' }"
      >
        <reject :rejectvalue="rejectvalue" @close="rejectcloseHandler" />
      </BaseDialog>
      <Drawer
        :visible.sync="detailVisible"
        :title="'订单信息'"
        destroyOnClose
        :containerStyle="{ height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px' }"
        width="72%"
      >
        <detailModel :rowData="formData" @close="emitcloseHandler" />
      </Drawer>
    </div>
  </Spin>
</template>
<script>
import { getList, exportList, updateStatusByIds,exporturl } from '@/api/omSale/OrderManagement/DirectSaleAudit/index.js';
import detailModel from './detailModel';
import ownerSeacher from './ownerSeacher';
import { exportExcelFile } from '@/utils';
import { getAppId } from '@/assets/js/auth';
import { dictionary } from '@/utils/dictMixin';
import reject from './reject';
export default {
  name: 'acknowledgementOrder',
  data() {
    this.BaseTable = null;
    return {
      topFilterList: this.createdFrom(),
      columns: this.createdTable(),
      fetchApi: getList,
      fetchParams: {
        xhrAbort: true
      },
      formData: {},
      exportLoading: false,
      issearch: false,
      detailVisible: false,
      ProjectId: '',
      selectedRows: [],
      loading: false,
      isreject: false,
      rejectvalue: ''
    };
  },
  components: {
    ownerSeacher,
    detailModel,
    reject
  },
  mixins: [dictionary],
  mounted() {
    this.BaseTable = this.$refs.table.$refs.pageTable;
  },
  created() {},
  methods: {
    createdFrom() {
      return [
        {
          type: 'INPUT',
          label: '经销商代码',
          fieldName: 'dealerCode',
          placeholder: '请选择',
          unitRender: () => {
            return (
              <div>
                <el-button icon="el-icon-search" onClick={() => this.seacherFun(1)}></el-button>
              </div>
            );
          }
        },
        {
          type: 'INPUT',
          label: '经销商名称',
          fieldName: 'dealerName',
          placeholder: '请输入'
        },

        {
          type: 'RANGE_DATE',
          label: '提交日期',
          fieldName: 'createDateStart|createDateEnd',
          placeholder: '请选择',
          initialValue: [],
          dateType: 'exactdaterange'
        },
        {
          type: 'INPUT',
          label: 'APP订单号',
          fieldName: 'appOrderNo',
          placeholder: '请输入'
        },
        {
          type: 'RANGE_DATE',
          label: '审核日期',
          fieldName: 'auditStartDate|auditEndDate',
          placeholder: '请选择',
          initialValue: [],
          dateType: 'exactdaterange'
        }
      ];
    },
    createdTable() {
      return [
        {
          title: '序号',
          dataIndex: 'index',
          width: 80,
          sorter: true,
          fixed: 'left',
          render: props => {
            return <span>{props.row.index + 1}</span>;
          }
        },
        {
          title: '操作',
          dataIndex: 'column-action',
          width: 50,
          fixed: 'left',
          render: (props, h) => {
            return (
              <div>
                <multiuse-button size="mini" type="text" onClick={() => this.editHandle(props.row, 2)}>
                  审核
                </multiuse-button>
              </div>
            );
          }
        },
        {
          title: '事业部',
          dataIndex: 'deptName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '办事处',
          dataIndex: 'officeName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '经销商代码',
          dataIndex: 'dealerCode',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '经销商名称',
          dataIndex: 'dealerName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '销售门店代码',
          dataIndex: 'xsDealerCode',
          hidden: getAppId() == 'beijing' ? true : false,
          sorter: true,
        },
        {
          title: '销售门店名称',
          dataIndex: 'xsDealerName',
          hidden: getAppId() == 'beijing' ? true : false,
          sorter: true,
        },

        {
          title: 'APP订单号',
          dataIndex: 'appOrderNo',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: 'VIN',
          dataIndex: 'vin',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '折扣金额',
          dataIndex: 'discountAmount',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '开票价格',
          dataIndex: 'ticketRates',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '品牌名称',
          dataIndex: 'brandName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '车系名称',
          dataIndex: 'seriesName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '车型名称',
          dataIndex: 'modelName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '配置名称',
          dataIndex: 'configName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '选装名称',
          dataIndex: 'optionName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '内饰',
          dataIndex: 'trimColor',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '外饰',
          dataIndex: 'colorName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '产品代码',
          dataIndex: 'productCode',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '产品名称',
          dataIndex: 'productName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '仓库名称',
          dataIndex: 'warehouseName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '客户姓名',
          dataIndex: 'customerName',
          sorter: true,
          filter: true,
          filterType: 'input'
          // filterType: 'checkbox',
          // filterItems: this.createDictList('6006'),
          // render: p => {
          //   return <span>{this.createDictText(p.row.orderType, '6006')}</span>;
          // }
        },
        {
          title: '组织机构代码/身份证号',
          dataIndex: 'customerCardNo',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '联系电话',
          dataIndex: 'linkMobile',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '邮政编码',
          dataIndex: 'postCode',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '联系地址',
          dataIndex: 'linkAddress',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '开票类型',
          dataIndex: 'billTypeStr',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '基本价（销售指导价）',
          dataIndex: 'basePrice',
          sorter: true,
          filter: true,
          filterType: 'input'
        },

        {
          title: '中央补贴',
          dataIndex: 'nationalSubsidy',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '地方补贴',
          dataIndex: 'citySubsidy',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '客户政策',
          dataIndex: 'customerPolicy',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '促定政策',
          dataIndex: 'promotePolicy',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '置换政策',
          dataIndex: 'substitutePolicy',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '单车政策',
          dataIndex: 'singlePolicy',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: 'VIP政策',
          dataIndex: 'vipPolicy',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '员工政策',
          dataIndex: 'staffPolicy',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '城市政策',
          dataIndex: 'cityPolicy',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '其他政策',
          dataIndex: 'otherPolicy',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '授信单号',
          dataIndex: 'creditNo',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '授信类型',
          dataIndex: 'creditTypeStr',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '单据状态',
          dataIndex: 'statusStr',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '提报时间',
          dataIndex: 'createdAt',
          sorter: true,
          filter: true,
          filterType: 'date-range'
        },
        {
          title: '审核时间',
          dataIndex: 'checkAt',
          sorter: true,
          filter: true,
          filterType: 'date-range'
        },
        {
          title: '订单备注',
          dataIndex: 'orderRemark',
          sorter: true,
          filter: true,
          filterType: 'input'
        }
      ];
    },
    selectChangeHandle(row) {
      this.selectedRows = row;
    },
    seacherFun(type) {
      if (type == 1) {
        this.issearch = true;
      } else {
        this.isAccountProject = true;
      }
    },
   async addHandle() {
      let json = Object.assign({}, { CODE: 'CODE_0000040' }, this.fetchParams);
      let res = await exporturl(json);
      if (res.resultCode == 200) {
        this.$router.push('/admin/auth/oem/downloadCenter');
      }

      // this.$confirm('是否将Excel表格保存至本地文件夹?', '提示', {
      //   confirmButtonText: '下载',
      //   cancelButtonText: '取消',
      //   type: 'warning'
      // })
      //   .then(() => {
      //     let json = this.fetchParams;
      //     this.exportLoading = true;

      //     exportList(json)
      //       .then(response => {
      //         console.log(response, 'response');
      //         exportExcelFile(response.data, '直营订单开票申请财务审核');
      //         this.exportLoading = false;
      //       })
      //       .catch(err => {
      //         console.log(err);
      //       });
      //   })
      //   .catch(() => {
      //     console.log('456');
      //     this.$message({
      //       type: 'info',
      //       message: '已取消'
      //     });
      //   });
    },
    changeHandle(params) {
      this.fetchParams.projectCode = this.ProjectId;
      this.fetchParams.xhrAbort = false;
      this.fetchParams = Object.assign({}, this.fetchParams, params);
    },
    collapseHandle() {
      this.$nextTick(() => {
        this.BaseTable.EXECUTE_RESET_HEIGHT();
      });
    },
    editHandle(data) {
      this.formData = data;
      this.detailVisible = true;
    },
    emitcloseHandler(type) {
      if (type) {
        this.detailVisible = false;
      }
      this.detailVisible = false;
      this.fetchParams = { ...this.fetchParams };
    },
    dealercloseHandler(val, data) {
      if (data) {
        let dealerCodes = [];
        data.map(x => {
          dealerCodes.push(x.dealerCode);
        });
        this.topFilterList.find(x => x.fieldName == 'dealerCode').initialValue = dealerCodes.toString();
      }
      this.issearch = false;
    },
    resetChange(val) {
      this.ProjectId = '';
      this.fetchParams.projectCode = '';
      this.fetchParams = Object.assign({}, this.fetchParams);
    },
    rejectcloseHandler(val) {
      if (val) {
        this.fetchParams = { ...this.fetchParams };
      }
      this.isreject = false;
    },
    async adoptHandle(val) {
      if (this.selectedRows.length > 0) {
        this.$confirm('是否确定批量审核？', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        })
          .then(async () => {
            let str = '';
            let flag = 0;
            this.selectedRows.map(x => {
              str += x.id + ',';
              if (x.status != 60631001) {
                flag = 1;
              } else {
                flag = 0;
              }
            });
            if (flag == 0) {
              if (val == 60631003) {
                this.isreject = true;
                this.rejectvalue = str;
              } else {
                this.loading = true;
                const res = await updateStatusByIds({ ids: str, status: val });
                if (res.resultCode == 200) {
                  this.$notify({
                    title: '成功',
                    message: '审核成功',
                    type: 'success'
                  });
                }
                this.loading = false;
                this.fetchParams = { ...this.fetchParams };
              }
            } else if (flag == 1) {
              this.loading = false;
              return this.$notify.warning({ title: '提示信息', message: '不能批量操作审核后的单据！' });
            }
          })
          .catch(() => {});
      } else {
        return this.$notify.warning({ title: '提示信息', message: '请勾选后再进行批量操作！' });
      }
    }
  }
};
</script>
